package com.cowcowit.springcloud.controller;

import com.cowcowit.springcloud.entity.Pay;
import com.cowcowit.springcloud.entity.PayDTO;
import com.cowcowit.springcloud.result.ResultData;
import com.cowcowit.springcloud.result.ReturnCodeEnum;
import com.cowcowit.springcloud.service.IPayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Tag(name = "支付微服务模块", description = "支付CRUD")
public class PayController {

    @Resource
    private IPayService payService;

    // 添加
    @PostMapping(value = "/pay/add")
    @Operation(summary = "新增", description = "新增支付流水方法,json串做参数")
    public ResultData<String> addPay(@RequestBody Pay pay) {
        try {
            payService.save(pay);
            return ResultData.success("添加成功");
        } catch (Exception e) {
            return ResultData.fail(ReturnCodeEnum.RC500.getCode(), e.getMessage());
        }
    }

    // 删除
    @Operation(summary = "删除", description = "删除支付流水方法")
    @DeleteMapping(value = "/{id}")
    public ResultData<String> deletePay(@PathVariable("id") Integer id) {
        try {
            payService.removeById(id);
            return ResultData.success("删除成功");
        } catch (Exception e) {
            return ResultData.fail(ReturnCodeEnum.RC500.getCode(), e.getMessage());
        }
    }

    // 更新
    @PutMapping(value = "/pay/update")
    @Operation(summary = "修改", description = "修改支付流水方法")
    public ResultData<String> updatePay(@RequestBody PayDTO payDTO) {
        try {
            Pay pay = new Pay();
            BeanUtils.copyProperties(payDTO, pay);
            payService.updateById(pay);
            return ResultData.success("修改成功");
        } catch (BeansException e) {
            return ResultData.fail(ReturnCodeEnum.RC500.getCode(), e.getMessage());
        }

    }

    // 根据id获取
    @GetMapping(value = "/pay/get/{id}")
    @Operation(summary = "按照ID查流水", description = "查询支付流水方法")
    public ResultData<Pay> getById(@PathVariable("id") Integer id) {
        try {
            Pay pay = payService.getById(id);
            return ResultData.success(pay);
        } catch (Exception e) {
            return ResultData.fail(ReturnCodeEnum.RC500.getCode(), e.getMessage());
        }
    }

    // 获取所有
    @Operation(summary = "按照所有查流水", description = "查询所有支付流水方法")
    @GetMapping(value = "/pay/get")
    public ResultData<List<Pay>> getAll() {
        try {
            List<Pay> list = payService.list();
            return ResultData.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.fail(ReturnCodeEnum.RC500.getCode(), e.getMessage());
        }
    }


    @Operation(summary = "猜测是异常", description = "猜测是异常")
    @RequestMapping(value = "/pay/error", method = RequestMethod.GET)
    public ResultData<Integer> getPayError() {
        Integer i = Integer.valueOf(200);
        try {
            System.out.println("--------come here");
            int data = 10 / 0;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.fail(ReturnCodeEnum.RC500.getCode(), e.getMessage());
        }
        return ResultData.success(i);
    }

    @Value("${server.port}")
    private String port;
    @GetMapping(value = "/pay/get/info")
    public String getInfoByConsul() {
        return "port: "+port;
    }
}
